package edu.whut.mall.user.controller;

import edu.whut.mall.common.vo.Page;
import edu.whut.mall.common.api.ResponseMap;
import edu.whut.mall.user.service.IAddressService;
import edu.whut.mall.user.vo.AddressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Dary
 * @projectName mall-refactor
 * @date 2021/6/28 16:28
 */

@RequiredArgsConstructor
@RequestMapping("/userAddress")
@RestController
@Api(value="收货地址",tags = {"收货地址"})

public class AddressController {

    @Autowired
    private IAddressService IAddressService;

//    @Autowired
//    private AddressMapper addressMapper;

    /**
     * 用户收货地址
     */
    @ApiOperation(value = "获取一条收货地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE,
            notes = "某个用户获取一条收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uaId", value = "地址记录id", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "userId", value = "固定传1，不传报错", paramType = "form", dataType = "int", required = true)
    })
    @RequestMapping(value = "/getOneUserAddress", method = RequestMethod.POST)
    public Map getOneUserAddress(HttpServletRequest request, int userId, int uaId) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        AddressVo userAddress = IAddressService.getOneUserAddress(uaId, userId);

        return map.putValue(userAddress == null ? "获取失败" : userAddress);
    }

    @ApiOperation(value = "新增一条收货地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE,
            notes = "某个用户新增一条收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "areaCode", value = "地区码", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "address", value = "详细街道,门牌号", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "realName", value = "真实姓名", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "form", dataType = "string", required = true),
    })

    @RequestMapping(value = "/addOneUserAddress", method = RequestMethod.POST)
    public Map addOneUserAddress(HttpServletRequest request, int userId, int areaCode, String address,
                                 String realName, String phone) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        AddressVo userAddress = IAddressService.addOneUserAddress(userId, areaCode, address, realName, phone);
        if (userAddress == null) {
            return map.putFailure("新增失败,用户已存在", 0);
        }
        return map.putValue(userAddress, "新增成功");
    }

    @ApiOperation(value = "编辑一条收货地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE,
            notes = "某个用户编辑一条收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "id", value = "uaId", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "areaCode", value = "地区码", paramType = "form", dataType = "int", required = false),
            @ApiImplicitParam(name = "address", value = "详细街道,门牌号", paramType = "form", dataType = "string", required = false),
            @ApiImplicitParam(name = "realName", value = "真实姓名", paramType = "form", dataType = "string", required = false),
            @ApiImplicitParam(name = "phone", value = "手机号", paramType = "form", dataType = "string", required = false),

    })
    @RequestMapping(value = "/updateOneUserAddress", method = RequestMethod.POST)
    public Map updateOneUserAddress(HttpServletRequest request, int userId, int id, Integer areaCode, String address,
                                    String realName, String phone) throws Exception {
        ResponseMap map = ResponseMap.getInstance();

        AddressVo userAddress = IAddressService.updateOneUserAddress(id,userId, areaCode, address, realName, phone);
        if (userAddress == null) {
            return map.putFailure("编辑失败,用户不存在", 0);
        }

        return map.putValue(userAddress, "编辑成功");
    }

    @ApiOperation(value = "删除收货地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE,
            notes = "用户删除一条或多条收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uaId", value = "", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", dataType = "int", required = true)
    })
    @RequestMapping(value = "/deleteAddress", method = RequestMethod.POST)
    public Map deleteAddress(HttpServletRequest request, int userId, int uaId) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        boolean userAddress = IAddressService.deleteOneUserAddress(userId, uaId);
        if (!userAddress) {
            return map.putFailure("删除失败或没有要删除的记录", 0);
        }
        return map.putSuccess("删除成功");
    }

    @ApiOperation(value = "设为默认收货地址", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE,
            notes = "用户将某个地址设为默认收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uaId", value = "地址记录id", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "form", dataType = "int", required = true)
    })
    @RequestMapping(value = "/updateUserAddressAsDefault", method = RequestMethod.POST)
    public Map updateUserAddressAsDefault(HttpServletRequest request, int userId, int uaId) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        boolean userAddress = IAddressService.updateOneUserAddressAsDefault(userId, uaId);
        if (!userAddress) {
            return map.putFailure("编辑失败", 0);
        }

        return map.putSuccess("编辑成功");
    }

    @ApiOperation(value = "获取收货地址列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE,
            notes = "某个用户获取收货地址列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "固定传1，不传报错", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "address", value = "当前页数", paramType = "form", dataType = "int", required = false),
            @ApiImplicitParam(name = "current", value = "当前页数", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "size", value = "当前页面显示记录数", paramType = "form", dataType = "int", required = true),
            @ApiImplicitParam(name = "orderBy", value = "排序依据，例如:create_time", paramType = "form", dataType = "string", required = true),
            @ApiImplicitParam(name = "asc", value = "排序规则，递增true或递减false", paramType = "form", dataType = "boolean", required = true)
    })
    @RequestMapping(value = "/getUserAddressPageList", method = RequestMethod.POST)
    public Map getUserAddressPageList(HttpServletRequest request, int userId,
                                      int current, int size, String address, String orderBy, Boolean asc) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        Page<AddressVo> userAddressPage = IAddressService.getUserAddressPageList(current, size, userId, address, orderBy, asc);
        return map.putPage(userAddressPage, "获取成功");
    }
}
